package com.sedna.recipenote.common.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sedna.recipenote.common.document.AppUser;
import com.sedna.recipenote.common.repository.AppUserRepository;
import com.sedna.recipenote.framework.value.UserValue;

@Service
public class UserService {
	Logger logger = Logger.getLogger(UserService.class);

	@Autowired
	AppUserRepository appUserRepository;

	@Transactional
	public AppUser signup(AppUser appUser) {
		logger.info("Inside createUser" + appUser.getLastName());
		return appUserRepository.save(appUser);
	}

	@Transactional
	public AppUser createUser(AppUser appUserDoc) {
		appUserRepository.save(appUserDoc);
		return appUserDoc;
	}

	@Transactional
	public AppUser updateUser(AppUser appUserDoc) {
		appUserRepository.save(appUserDoc);
/*		int a = 1;
		if(a==1) {
			throw new NullPointerException("Testing... Throwing exception manually");
		}*/
		return appUserDoc;
	}

	public UserValue getCurrentUser() {
		UserValue userValue = (UserValue) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return userValue;
	}
}